/***********************************************************************************
Copy right:	    2016-2026 USR IOT Tech.
Author:         dingning
Date:           2021.9.3
Description:    
***********************************************************************************/

#include "sn_uart.h"
#include <rtthread.h>
#include <board.h>
#include <rtdevice.h>


static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    //rt_kprintf("uart_input size:%d",size);
    return RT_EOK;
}

void uart_service_thread_entry(void *para)
{
	rt_kprintf("uart_service_thread_entry!\n");
	struct uart_para *upara = (struct uart_para *)para;
	struct uart_recv_msg rmsg={0};
	struct uart_recv_msg rmsg1={0};
	int ret=0;
	/* 串口设备句柄 */
	static rt_device_t serial;                
	/* 查找串口设备 */
	
	serial = rt_device_find(upara->name);
	if(serial!= RT_NULL)
	{	
		rt_kprintf("find uart:%s.\n",upara->name);
	}
	
	ret = rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
	if(ret != RT_EOK)
	{
		rt_kprintf("open %s failed.\n",upara->name);
	}

	//rt_device_set_rx_indicate(serial, uart_input);
	
	char str[] = "hello RT-Thread!\r\n";
	rt_device_write(serial, 0, str, (sizeof(str) - 1));
	char read_str[512]={0};
	char buff[1024]={0};
	int size =0;
	int buff_size =0;
	while (1)
	{
		buff_size = 0;
		rt_memset(read_str,0,512);
		rt_memset(buff,0,1024);
		while((size = rt_device_read(serial, 0, read_str, 512))>0)
		{			
			//rt_kprintf("%s read size:%d,%s",upara->name,size,read_str);
			rt_kprintf("%s read size:%d\n",upara->name,size);
			if((buff_size + size)>1024)
			{
				break;
			}
			rt_memcpy(buff+buff_size,read_str,size);
			buff_size += size;
			rt_thread_mdelay(5);	
		}
		if(buff_size > 0)
		{
			buff[buff_size] = '\0';
			//rt_kprintf("%s buff_size:%d,%s",upara->name,buff_size,buff);
			rt_kprintf("%s buff_size:%d\n",upara->name,buff_size);
			//rt_device_write(serial, 0, buff, buff_size);
			if(buff_size>256)
			{
				buff_size=256;
			}
			buff[buff_size] = 0;
			rt_memcpy(rmsg.buff,buff,buff_size);
			rmsg.buff[buff_size] = 0;
			rt_kprintf("rmsg.buff:%s\n",rmsg.buff);
			rmsg.size = buff_size;
			ret = rt_mq_send(upara->mq, &rmsg, sizeof(struct uart_recv_msg));
            if (ret != RT_EOK)
            {
                rt_kprintf("%s rt_mq_send err.\n",upara->name);
            }
			rt_kprintf("rt_mq_send success.\n");
            // rt_kprintf("thread2: send message \n");	
			// if (rt_mq_recv(upara->mq, &rmsg1, sizeof(rmsg1), RT_WAITING_FOREVER) == RT_EOK)
			// {
			// 	rt_kprintf("thread1: recv msg from msg queue, the content:%s\n", rmsg1.buff);
			// 	rt_device_write(serial, 0, rmsg1.buff, rmsg1.size);
			// }	
		}
		else
		{
			rt_thread_mdelay(10);	
		}
	}
}
#define SAMPLE_UART_NAME       "uart1"    /* 串口设备名称 */
struct uart_para uart0_para={0};
int uart_service_init(void)
{	
	uart0_para.name = SAMPLE_UART_NAME;
	char name[32] = {0};
	rt_sprintf(name,"%s_mq",uart0_para.name);
	uart0_para.mq = rt_mq_create(name,sizeof(struct uart_recv_msg),3, RT_IPC_FLAG_PRIO);
	
	rt_thread_t uart_service_thread = rt_thread_create("uart_service_thread", uart_service_thread_entry, &uart0_para, 4096,15, 20);
	if (uart_service_thread == RT_NULL)
	{
		rt_kprintf("create uart_service_thread failed.\n");
		return RT_ERROR;
	}
	else
	{
		rt_kprintf("create uart_service_thread success.\n");
		rt_thread_startup(uart_service_thread);
		return RT_EOK;
	}
}
MSH_CMD_EXPORT(uart_service_init, "uart_service_init")

